package net.minecraft.server;

import java.util.ArrayList;
import java.util.Random;
import org.bukkit.BlockChangeDelegate;

public class CustomObjectGen extends WorldGenerator
{
  private WorldWorker localWrk;

  public CustomObjectGen(WorldWorker wrk)
  {
    this.localWrk = wrk;
  }

  public boolean a(World world, Random random, int i, int j, int k)
  {
    int SelectedObject = random.nextInt(this.localWrk.Spawner.Objects.size());
    CustomObject Object = (CustomObject)this.localWrk.Spawner.Objects.get(SelectedObject);
    return generate((BlockChangeDelegate)world, random, i, j, k, Object);
  }

  public boolean CustomObjectBuilder(World world, Random random, int i, int j, int k, BiomeBase localBiomeBase)
  {
    if (this.localWrk.Spawner.Objects.size() > 0)
    {
      int SelectedObject = random.nextInt(this.localWrk.Spawner.Objects.size());
      CustomObject Object = (CustomObject)this.localWrk.Spawner.Objects.get(SelectedObject);
      return generate((BlockChangeDelegate)world, random, i, j, k, Object);
    }
    return false;
  }

  public boolean generate(BlockChangeDelegate world, Random random, int i, int j, int k, CustomObject Object)
  {
	    
	      int i1 = world.getTypeId(i, j - 1, k);
	      if ((j < 128 - 1)) {
	    	  
	      if ((i1 == Block.GRASS.id))
	      {
	          world.setRawTypeId(i, j - 1, k, Block.DIRT.id);
	      }
	      if ((world.getTypeId(i, j + 2, k) != Block.WATER.id) || (Object.underwater))
	      {
	       
	          int j1 = world.getTypeId(i, j - 1, k);
	          if ((j1 == ((byte)Object.spawnID)))
	          {
	        	  int DataX = 0;
	        	  int DataY = 0;
	        	  int DataZ = 0;
	              while (DataZ <= 100)
	              {
	                  while (DataY <= 14)
	                  {
	                      while (DataX <= 14)
	                      {
	                          if (Object.DataValues[DataX][DataY][DataZ] !=0)
	                          {
	                        	  
	                        	  int Data = 0;
	                        	  int eData = 0;
	                        	  Data = (int)Object.DataValues[DataX][DataY][DataZ];
	                        	  eData = (int)Math.round(((Object.DataValues[DataX][DataY][DataZ] - Data) * 100));
	                        	  if (!(Data == 0))
	                        	  {
	                        	  world.setRawTypeIdAndData(i + (DataX - 4), (j - 1) + (DataZ), k + (DataY - 4), Data, eData);
	                        	  if (DataZ == 0)
	                        	  {
	                        		  int DownChecker = 1;
	                        		  while (world.getTypeId(i + (DataX -4), (j - DownChecker), k + (DataY - 4)) == 0)
	                        				  {
	                        			  		world.setRawTypeId(i + (DataX -4), (j - DownChecker), k + (DataY - 4), (byte)Object.spawnID);
	                        				  }
	                        	  }
	                        	  }
	                          }
	                          DataX = DataX + 1;
	                      }
	                      DataX = 0;
	                      DataY = DataY + 1;
	                  }
	  				DataY =0;
	                  DataZ = DataZ + 1;
	                          }
		      return true;
	          }
	        
	                 }
	        
}
	      return false;
  }
}
  
  